﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Microsoft.Telco.Web.Services.ServiceModel.Dispatcher
{

	/// <summary>
	/// Message inspector for service call messages.
	/// </summary>
	/// <remarks>
	/// Sets the response code to HTTP 200 for fault conditions so that Silverlight clients can process successfully.
	/// </remarks>
	public class SilverlightFaultMessageInspector : IDispatchMessageInspector
	{

		/// <summary>
		/// Called after the operation has returned but before the reply message is sent.
		/// </summary>
		/// <param name="reply">The reply message. This value is null if the operation is one way.</param>
		/// <param name="correlationState">The correlation object returned from the <see cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest(System.ServiceModel.Channels.Message@,System.ServiceModel.IClientChannel,System.ServiceModel.InstanceContext)"/> method.</param>
		public void BeforeSendReply(ref Message reply, object correlationState)
		{
			if (reply.IsFault)
			{
				HttpResponseMessageProperty property = new HttpResponseMessageProperty();

				// Here the response code is changed to 200.
				property.StatusCode = System.Net.HttpStatusCode.OK;

				reply.Properties[HttpResponseMessageProperty.Name] = property;
			}
		}

		/// <summary>
		/// Called after an inbound message has been received but before the message is dispatched to the intended operation.
		/// </summary>
		/// <param name="request">The request message.</param>
		/// <param name="channel">The incoming channel.</param>
		/// <param name="instanceContext">The current service instance.</param>
		/// <returns>
		/// The object used to correlate state. This object is passed back in the <see cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply(System.ServiceModel.Channels.Message@,System.Object)"/> method.
		/// </returns>
		public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
		{
			// No-op on incoming message.
			return null;
		}
	}

}
